package android.sms.auto.responder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SMSListener extends BroadcastReceiver implements LocationListener{

	
	
	

	public LocationManager lm;
	public String lon;
	public String lat;
	


	@SuppressWarnings("static-access")
	@Override
	public void onReceive(Context arg0, Intent arg1) {
		try{
		lm = (LocationManager)arg0.getSystemService(arg0.LOCATION_SERVICE);
		lm.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            this);
		SharedPreferences passwdfile = arg0.getSharedPreferences(AndroidSmsAutoResponder.PASSWORD_PREF_KEY, 0);
		String correctMd5 = passwdfile.getString(AndroidSmsAutoResponder.PASSWORD_PREF_KEY,
				null);
		if (correctMd5 != null) {
			
			Bundle bundle = arg1.getExtras();
			Object[] pdus = (Object[] )bundle.get("pdus");
			SmsMessage[] messages = new SmsMessage[pdus.length];//aloc memorie in functie de nr de sms-uri 
			/*urmeaza copierea continutului*/
			for (int i=0; i<messages.length; i++){
				messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
			}
			//analizare continut mesaje
			for (SmsMessage msg : messages) {
				if (msg.getMessageBody().contains("SMSLOCATE:")) {
					String[] tokens = msg.getMessageBody().split(":");
					if (tokens.length >= 2) {
						String md5hash = AndroidSmsAutoResponder.getMd5Hash(tokens[1]);

						if (md5hash.equals(correctMd5)) {
							Toast.makeText(arg0,"Correct password, sending myLocation" , 
									Toast.LENGTH_LONG).show();
							String to = msg.getOriginatingAddress();
							if(lm.isProviderEnabled("gps"))
							{
								
								SmsManager sm = SmsManager.getDefault();
								lat = String.valueOf(this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude());
								lon = String.valueOf(this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude());
								String detalii = "Latitudine: " + this.lat + " Longitudine: " + this.lon;
								sm.sendTextMessage(to,null, detalii, null, null);
								Toast.makeText(arg0,"Location details delivered to "+to ,1000000000).show();
							}
						}
							
					}
				}
			}
		}
	}catch(Exception e){
		Toast.makeText(arg0, e.toString()+" "+e.getClass().toString(), 
				1000000000).show();
		Log.e("EXCEPTIE", e.toString());
	}
	}

	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		if (arg0 != null) {                
            this.lat = String.valueOf(arg0.getLatitude());
    		this.lon = String.valueOf(arg0.getLongitude());
		}

		
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		

	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}
}

	

		
	


